package com.oig.sys.auth.service.impl;

import com.oig.sys.auth.dao.ITbOpenUserDao;
import com.oig.sys.auth.dto.OpenUserDto;
import com.oig.sys.auth.entity.TbOpenUser;
import com.oig.sys.auth.service.ITbOpenUserService;
import com.oig.sys.auth.vo.OpenUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wuxl
 */
@Service
public class TbOpenUserServiceImpl implements ITbOpenUserService {

	private final ITbOpenUserDao tbOpenUserDao;

	@Autowired
	public TbOpenUserServiceImpl(ITbOpenUserDao tbOpenUserDao){
		 this.tbOpenUserDao = tbOpenUserDao;
	}

	@Transactional
	@Override
	public void addOrUpdate(OpenUserDto openUserDto) {
		TbOpenUser openUser = this.tbOpenUserDao.getByUserIdAndOpenAuthId(openUserDto.getUserId(), openUserDto.getOpenAuthId());
		if (openUser==null){
			openUser = new TbOpenUser();
		}
		BeanUtils.copyProperties(openUserDto, openUser, "id");
		this.tbOpenUserDao.save(openUser);
	}

	@Override
	public OpenUserVo getOpenUserInfo(Integer userId) {
		return this.buildVo(tbOpenUserDao.getByUserId(userId));
	}

	private OpenUserVo buildVo(TbOpenUser openUser){
		if (openUser==null){
			return null ;
		}
		OpenUserVo openUserVo = new OpenUserVo();
		BeanUtils.copyProperties(openUser, openUserVo);
		return openUserVo;
	}
}
